package com.zjs.express.service.impl;

import com.zjs.express.bean.Express;
import com.zjs.express.bean.sms.SmsBean;
import com.zjs.express.bean.sms.SmsMessage;
import com.zjs.express.bean.vo.ExpressCountVo;
import com.zjs.express.bean.vo.ExpressVo;
import com.zjs.express.common.ResultModel;
import com.zjs.express.dao.ExpressDao;
import com.zjs.express.dao.impl.ExpressDaoImpl;
import com.zjs.express.service.ExpressService;
import com.zjs.express.utils.ALiYunSmsUtils;
import com.zjs.express.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class ExpressServiceImpl implements ExpressService {
    private ExpressDao expressDao = new ExpressDaoImpl();

    @Override
    public ExpressCountVo statistics() {
        return expressDao.statistics();
    }

    @Override
    public ResultModel<List<ExpressVo>> list(Integer pageIndex, Integer pageSize) {
        Integer total = expressDao.getTotal();
        List<Express> lists = expressDao.list(pageIndex, pageSize);
        List<ExpressVo> list = new ArrayList<>();
        lists.forEach(express -> {
            ExpressVo expressVo = new ExpressVo();
            expressVo.setId(express.getId());
            expressVo.setNumber(express.getNumber());
            expressVo.setUsername(express.getUsername());
            expressVo.setUserPhone(express.getUserPhone());
            expressVo.setCompany(express.getCompany());
            expressVo.setCode(express.getCode());
            expressVo.setInTime(express.getInTime());
            expressVo.setOutTime(express.getOutTime());
            expressVo.setStatus(express.getStatus());
            expressVo.setSysPhone(express.getSysPhone());
            expressVo.setInTimeStr(DateUtils.formatDate(express.getInTime()));
            expressVo.setOutTimeStr(DateUtils.formatDate(express.getOutTime()));
            list.add(expressVo);
        });
        return ResultModel.success(list, total);
    }

    @Override
    public ResultModel add(Express express) {
        boolean result = expressDao.add(express);
        if (result) {
            //短信通知
            SmsBean smsBean = null;
            try {
                SmsMessage smsMessage = new SmsMessage(express.getCode(), express.getCompany(), express.getSysPhone());
                smsBean = new SmsBean(express.getUserPhone(), smsMessage, "SMS_468975358");
                ALiYunSmsUtils.sendSms(smsBean);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResultModel.success();
        }
        return ResultModel.failure("快递录入失败！");
    }

    @Override
    public ResultModel<ExpressVo> listByNumber(String number) {
        Express express = expressDao.listByNumber(number);
        if (express != null) {
            ExpressVo expressVo = new ExpressVo();
            expressVo.setId(express.getId());
            expressVo.setNumber(express.getNumber());
            expressVo.setUsername(express.getUsername());
            expressVo.setUserPhone(express.getUserPhone());
            expressVo.setCompany(express.getCompany());
            expressVo.setCode(express.getCode());
            expressVo.setInTime(express.getInTime());
            expressVo.setOutTime(express.getOutTime());
            expressVo.setStatus(express.getStatus());
            expressVo.setSysPhone(express.getSysPhone());
            expressVo.setInTimeStr(DateUtils.formatDate(express.getInTime()));
            expressVo.setOutTimeStr(DateUtils.formatDate(express.getOutTime()));
            return ResultModel.success(expressVo);
        }
        return ResultModel.failure("快递不存在！");
    }

    @Override
    public ResultModel update(Express express) {
        boolean result = expressDao.update(express);
        if (result) {
            //短信通知
            SmsBean smsBean = null;
            try {
                SmsMessage smsMessage = new SmsMessage(express.getCode(), express.getCompany(), express.getSysPhone());
                smsBean = new SmsBean(express.getUserPhone(), smsMessage, "SMS_468975358");
                ALiYunSmsUtils.sendSms(smsBean);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResultModel.success();
        }
        return ResultModel.failure("快递修改失败！");
    }
}
